package com.huicoo.forestmanager.ui.patrol;

import android.content.Intent;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.huicoo.forestmanager.R;
import com.huicoo.forestmanager.base.BaseFragment;
import com.huicoo.forestmanager.network.HttpService;
import com.huicoo.forestmanager.network.bean.patrol.GetrecordschemeBean;
import com.huicoo.forestmanager.ui.patrol.forestCampTask.ForestCampTaskActivity;
import com.huicoo.forestmanager.util.JsonUtils;
import com.huicoo.forestmanager.util.LogUtils;
import com.huicoo.forestmanager.util.toast.ToastUtils;
import com.huicoo.forestmanager.widget.LoadingDialog;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 巡查方案
 */
public class PatrolFragmentVersion2 extends BaseFragment {


    @BindView(R.id.head_iv)
    ImageView map_view;

    @BindView(R.id.started_patrol)
    TextView started_patrol;

    @BindView(R.id.patrol_record)
    TextView patrol_record;

    @BindView(R.id.bottom_tv)
    TextView bottom_tv;

    private LoadingDialog loadingDialog;
//    private MapController mMapController;
//    private MyLocationOverlay myLocationOverlay;

    public static PatrolFragmentVersion2 getInstance() {
        return new PatrolFragmentVersion2();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_patrol_version_2;
    }

    @Override
    protected void initView() {

        //初始化地图设置
//        initMapView();

        //初始化数据
        initData();

    }

    //初始化地图设置
//    private void initMapView() {
//        //设置启用内置的缩放控件
//        map_view.setBuiltInZoomControls(false);
//
//        //得到mMapView的控制权,可以用它控制和驱动平移和缩放
//        mMapController = map_view.getController();
//
//        //创建MyLocationOverlay
//        myLocationOverlay = new MyLocationOverlay(getActivity(), map_view);
//
//        //启用指南针位置更新
//        myLocationOverlay.enableCompass();
//
//        //启用我的位置
//        myLocationOverlay.enableMyLocation();
//
//        map_view.addOverlay(myLocationOverlay);
//
//        //设置地图zoom级别
//        mMapController.setZoom(15);
//
//        //矢量
//        map_view.setMapType(MAP_TYPE_VEC);
//
//        //定位到当前位置
//        locateCurrentLocation();
//    }

    //定位到当前位置
//    private void locateCurrentLocation() {
//        //获得当前位置
//        GeoPoint myLocation = myLocationOverlay.getMyLocation();
//
//        //设置地图中心点
//        mMapController.setCenter(myLocation);
//
//        //动画移动到当前位置
//        mMapController.animateTo(myLocation);
//    }

    //初始化数据
    private void initData() {
        if (loadingDialog == null) {
            loadingDialog = new LoadingDialog(getActivity());
            loadingDialog.show();
        }
        Call<GetrecordschemeBean> call = HttpService.getInstance().getrecordscheme();
        call.enqueue(new Callback<GetrecordschemeBean>() {
            @Override
            public void onResponse(Call<GetrecordschemeBean> call, Response<GetrecordschemeBean> response) {
                //数据请求成功
                LogUtils.e("巡查方案 请求成功 : " + JsonUtils.toJson(response.body()));
                if (response.body() == null) {
                    //数据请求失败
                    ToastUtils.show("请求结果为空");
                    return;
                }
                GetrecordschemeBean body = response.body();
                if (TextUtils.equals(body.getCode(), "200")) {
                    String tiem = "";
                    if (body.getData().getType().contains("month")) {
                        tiem = "月";
                    }
                    if (body.getData().getType().contains("week")) {
                        tiem = "周";
                    }
                    if (body.getData().getType().contains("day")) {
                        tiem = "天";
                    }
                    bottom_tv.setText("上级要求巡查频率:每" + tiem + body.getData().getFrequency() + "次");
                } else {
                    ToastUtils.show(TextUtils.isEmpty(body.getErrorMsg()) ? "" : body.getErrorMsg());
                }
                loadingDialog.dismiss();
            }

            @Override
            public void onFailure(Call<GetrecordschemeBean> call, Throwable t) {
                loadingDialog.dismiss();
                LogUtils.v("请求 巡查方案 失败");
                t.printStackTrace();
            }
        });
    }


    //开始巡查
    @OnClick(R.id.started_patrol)
    public void startedPtrol() {
        // 开始巡查
        Intent intent = new Intent(getActivity(), ForestCampTaskActivity.class);
        startActivity(intent);
    }


    //巡查记录
    @OnClick(R.id.patrol_record)
    public void patrolRecord() {
        PatrolRecordActivity.start(getActivity());
    }


}
